package com.stylefeng.guns.rest.user;

import com.alibaba.dubbo.config.annotation.Reference;
import com.huangliusong.api.user.UserAPI;
import com.huangliusong.api.user.VO.UserInfoModel;
import com.huangliusong.api.user.VO.UserModel;
import com.stylefeng.guns.rest.common.CurrentUser;
import com.stylefeng.guns.rest.modular.VO.ResponseVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author huangliusong
 * @since 2019/1/12
 * {@link }
 */

@RequestMapping("/user/")
@RestController
public class UserController {


    @Reference(interfaceClass = UserAPI.class)
    public UserAPI userAPI;


    @RequestMapping(value = "register",method = RequestMethod.POST)
    public ResponseVO register(UserModel userModel){
        if (userModel.getUsername()==null||userModel.getUsername().trim().length()==0){
            return ResponseVO.serviceFail("用户名不能为空！");
        }

        if (userModel.getPassword()==null||userModel.getPassword().trim().length()==0){
            return ResponseVO.serviceFail("密码不能为空！");
        }
        boolean isSuccess=userAPI.register(userModel);
        if (isSuccess){
            return ResponseVO.success("注册成功！");
        }else {
            return ResponseVO.success("注册失败！");
        }

    }

    @RequestMapping(value = "check",method = RequestMethod.POST)
    public ResponseVO check(@RequestParam("username") String username){
        System.out.println("check->"+username);
       if (username!=null&&username.trim().length()>0){
            boolean notExist=userAPI.checkUsername(username);
            if (notExist){
                return ResponseVO.success("用户名不存在");
            }else {
                return ResponseVO.serviceFail("用户名已存在");
            }
       }else {
           return ResponseVO.serviceFail("用户名不能为空");
       }

    }


    @RequestMapping(value = "logout",method = RequestMethod.GET)
    public ResponseVO logout(String username){
        /*
        1。前端存储JWT 七天 jwt刷新问题
        2。服务器回存储用户活动信息
        3。jwt里的userid为key 查找活跃用户
        退出：
        1。前端删除jwt
        2。后段服务删除活跃用户
        现状：
        1。前端删除jwt

         */

        return  ResponseVO.success("用户退出成功！");
    }


    @RequestMapping(value = "getUserInfo",method = RequestMethod.GET)
    public ResponseVO  getUserInfo(){
        //获取当前的登陆用户

        String userID=CurrentUser.getCurrentUser();
        if (userID!=null&&userID.trim().length()>0){
                int uuid=Integer.parseInt(userID);
               UserInfoModel userInfoModel= userAPI.getUserInfo(uuid);
                if (userInfoModel!=null){
                    return ResponseVO.success(userInfoModel);
                }else{
                    return ResponseVO.appFail("用户信息查询失败");
                }
        }else{
            return ResponseVO.success("用户未登录");
        }
        //将用户的id传入到后段进行查询
    }



    @RequestMapping(value = "updateUserInfo",method = RequestMethod.POST)
    public ResponseVO  updateUserInfo(UserInfoModel userInfoModel){
        //获取当前的登陆用户

        String userID=CurrentUser.getCurrentUser();
        if (userID!=null&&userID.trim().length()>0){
            int uuid=Integer.parseInt(userID);
            if (uuid==userInfoModel.getUuid()){
                return ResponseVO.serviceFail("请修改您个人的信息");
            }
            UserInfoModel userInfo= userAPI.updateUserInfo(userInfoModel);
            if (userInfoModel!=null){
                return ResponseVO.success(userInfoModel);
            }else{
                return ResponseVO.appFail("用户信息修改失败");
            }
        }else{
            return ResponseVO.success("用户未登录");
        }
        //将用户的id传入到后段进行查询
    }
}
